package com.comdo.newup_loan.module.flow;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.adapter.TradeFlowAdapter;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.model.FlowListBean;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.widget.MyItemDecoration;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/11/9.
 */

public class TradeFlowActivity extends BaseActivity<TradeFlowPersenter> implements TradeFlowContact.View {
    @BindView(R.id.ll_tip)
    LinearLayout mTipLayout;
    @BindView(R.id.img_delete)
    ImageView mDeleteImg;
    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private TradeFlowAdapter mAdapter;
    private ArrayList<FlowListBean.FlowBean> listData = new ArrayList<FlowListBean.FlowBean>();
    private int page = 1;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_trade_flow;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(TradeFlowActivity.this, "查看流水", true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(TradeFlowActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new MyItemDecoration());
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mPresenter.getList(page);
            }

            @Override
            public void onLoadMore() {
                page++;
                mPresenter.getList(page);
            }
        });
        listData = new ArrayList<FlowListBean.FlowBean>();
        mAdapter = new TradeFlowAdapter(listData, TradeFlowActivity.this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.refresh();
        mDeleteImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTipLayout.setVisibility(View.GONE);
            }
        });


    }

    @Override
    public TradeFlowPersenter getmPresenter() {
        return new TradeFlowPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
        mRecyclerView.refreshComplete();
    }

    @Override
    public void getList(List<FlowListBean.FlowBean> datas) {
        Log.e("page",page+"");
        if(page==1)
        listData.clear();
        listData.addAll(datas);
        mAdapter.notifyDataSetChanged();
        mRecyclerView.refreshComplete();
       // if (datas.size() < Config.pageSize)
       //     mRecyclerView.setNoMore(true);
    }

}
